﻿var eCommerce = {
    Domain: 'http://locahost:6969',
    version: '1.0.0.0.0',
    LstCategory: [],
    LstPropertyValue: [],
    Sort: 'UnitPrice ASC',
    PageIndex: 1,
    PageSize: 12
}; 

/***************************************
= Document Ready
-------------------------------------- */
$(document).ready(function () {
    // hide #back-top first
    $("#back-top").hide();

    // fade in #back-top
    $(window).scroll(function () {
        if ($(this).scrollTop() > 100) {
            $('#back-top').fadeIn();
        } else {
            $('#back-top').fadeOut();
        }
    });

    // scroll body to 0px on click
    $('#back-top a').click(function () {
        $('body,html').animate({
            scrollTop: 0
        }, 800);
        return false;
    });

    //listCart();

//    listComment($('#hdArticleId').val(), $('#hdTypeId').val(), eCommerce.PageIndex);

//    $('#btnSend').click(function () {
//        createComment($('#hdArticleId').val(), $('#hdTypeId').val());
//    });
});

/***************************************
= Add & Remove Item In Cart
-------------------------------------- */
function listCart() {
    $('#ac_background').html('<div class="ac_loading"></div>'); // <div class="ac_overlay"></div>    
    var url = String.format('/Ajax/Common/SmallCart.aspx');
    $.ajax({
        url: url,
        data: {},
        type: 'get',
        success: function (result) {
            if (result.length > 0) {
                $('#back-top a').click();
                $('#smallCart').html(result);
                $('#ac_background').html('');
            }
        }
    });
}

function addItem(pId) {
    var url = String.format('/Handlers/eCommerceHandler.ashx?command=additem&id={0}', pId);
    $.post(url, function (data) {
        if (data != "success")
            alert("Không thêm được vào giỏ hàng. Vui lòng quay lại sau!");
    });

    listCart();
}

function removeItem(pId) {
    var url = String.format('/Handlers/eCommerceHandler.ashx?command=removeitem&id={0}', pId);
    $.post(url, function (data) {
        if (data != "success")
            alert("Không xóa được khỏi giỏ hàng. Vui lòng quay lại sau!");
    });

    listCart();
}

/***************************************
= Get List Search Product Value
-------------------------------------- */
function getPropertyValue(id) {
    if (eCommerce.LstPropertyValue.indexOf(id) == -1) {
        eCommerce.LstPropertyValue.push(id);
    }
    else {
        var index = eCommerce.LstPropertyValue.indexOf(id);
        eCommerce.LstPropertyValue.splice(index, 1);
    }

    var orderBy = $('#ddlSort').val();
    var pageSize = $('#ddlPageSize').val();
    getListFilterProduct(eCommerce.LstCategory, eCommerce.LstPropertyValue, orderBy, eCommerce.PageIndex, pageSize);    
}

function OnChangePropertyValue()
{
    var orderBy = $('#ddlSort').val();
    var pageSize = $('#ddlPageSize').val();

    $('#ddlSort').val(orderBy);
    $('#ddlPageSize').val(pageSize);

    getListFilterProduct(eCommerce.LstCategory, eCommerce.LstPropertyValue, orderBy, eCommerce.PageIndex, pageSize);    
}

/***************************************
= Get List Filter Product
-------------------------------------- */
function getListFilterProduct(lstCategory, lstPropertyValue, orderBy, pageIndex, pageSize) {
    var cat_url = $('#hdCatUrl').val();
    $('#ac_background').html('<div class="ac_loading"></div>'); // <div class="ac_overlay"></div>    
    var url = String.format('/Ajax/Product/Filter.aspx?lstCategory={0}&lstPropertyValue={1}&orderBy={2}&pi={3}&ps={4}&cat_url='+cat_url, 
                lstCategory, lstPropertyValue, orderBy, pageIndex, pageSize);    
    $.ajax({
        url: url,
        data: {},
        type: 'get',
        success: function (result) {
            if (result.length > 0) {
                $('#filterProduct').html(result);
                $('#ac_background').html('');
               // $('a.tip').tipsy({ fade: true, gravity: 's' });

                $('body,html').animate({
                    scrollTop: 0
                }, 800);
            }                            
        }
    });
}

/***************************************
= Comment
-------------------------------------- */

function createComment(Id, tId) {
    var urlGuide = String.format('/GetRequest.ashx?m=comment&action=create&Id={0}&tId={1}&username={2}&email={3}&content={4}', Id, tId, $('#txtCFullName').val(), $('#txtCEmail').val(), $('#txtCBody').val());
    $.getJSON(urlGuide, function (res) {
        if (res.Success) {
            alert('Bình luận của bạn đã được gửi thành công.');            
        }
    });
}

function listComment(id, typeId, pageIndex) {
    var url = String.format('/Ajax/Comment/ListByArticle.aspx?id={0}&tId={1}&pi={2}', id, typeId, pageIndex);
    $.ajax({
        url: url,
        data: {},
        type: 'get',
        success: function (result) {
            if (result.length > 0)
                $('#comment-article').html(result);
        }
    });
}

/***************************************




= String Format
-------------------------------------- */
//#region
String.format = function (text) {
    //check if there are two arguments in the arguments list
    if (arguments.length <= 1) {
        //if there are not 2 or more arguments there's nothing to replace
        //just return the original text
        return text;
    }

    //decrement to move to the second argument in the array
    var tokenCount = arguments.length - 2;
    for (var token = 0; token <= tokenCount; token++) {
        //iterate through the tokens and replace their placeholders from the original text in order
        text = text.replace(new RegExp("\\{" + token + "\\}", "gi"),
                                                arguments[token + 1]);
    }
    return text;
};
//#endregion